package cn.liccshop.core.service.dataCenter.service;

import cn.liccshop.sys.cache.CommonCacheUtil;
import cn.liccshop.sys.utils.DateNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiCC
 * @program LiCCShop
 * @description 数据中心Service实现类
 * @since 2019-05-27-01:59
 */
@Service
public class DataCenterServiceImpl implements IDataCenterService {
    @Autowired
    private CommonCacheUtil cacheUtil;
    @Override
    public Map<String, Object> getGoodsSalesData(String beginDate, String endDate) {
        List<String> dateList = DateNumberUtil.betweenYearMonth(beginDate, endDate);
        List<String> columns = new ArrayList<>();
        columns.add("年月");
        columns.add("销售量");
        columns.add("销售额（百）");
        columns.add("下单量");
        Long goodsVolume = new Long(0);
        BigDecimal goodsPrice = new BigDecimal(0);
        Long orderVolume = new Long(0);
        List<Map<String, Object>> rows = new ArrayList<>();
        for (String date : dateList){
            Map<String, Object> row = new HashMap<>();
            row.put(columns.get(0), date);
            row.put(columns.get(1), cacheUtil.getGoodsSalesVolume(date));
            goodsVolume = goodsVolume.longValue() + ((Long)row.get(columns.get(1))).longValue();
            BigDecimal temp = cacheUtil.getGoodsSalesPrice(date).divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP);
            row.put(columns.get(2), temp);
            goodsPrice = goodsPrice.add((BigDecimal) row.get(columns.get(2)));
            row.put(columns.get(3), cacheUtil.getOrderSalesVolume(date));
            orderVolume = orderVolume.longValue() + ((Long)row.get(columns.get(3))).longValue();
            rows.add(row);
        }
        Map<String, Object> data = new HashMap<>();
        data.put("columns", columns);
        data.put("rows", rows);
        data.put("goodsVolume", goodsVolume);
        data.put("goodsPrice", goodsPrice);
        data.put("orderVolume", orderVolume);
        return data;
    }
}
